package com.vhall.component.controller.data;

import com.alibaba.fastjson.JSON;
import com.vhall.component.controller.data.facade.DataExportFacade;
import com.vhall.component.entity.data.vo.DataExportParamVO;
import com.vhall.component.entity.data.vo.ExportListRspVO;
import com.vhall.component.entity.data.vo.ExportMessageReqVO;
import com.vhall.component.entity.data.vo.ExportReservationReqVO;
import com.vhall.component.entity.export.vo.ExportUvPreviewPageReqVO;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.constant.RedisKey;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.TokenThreadLocal;
import com.vhall.component.service.data.DataExportService;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;

/**
 * @author guoxin.wang
 * @date 2021-06-18 10:36
 */
@VhallComponent
public class DataExportComponent implements DataExportFacade {

    @Autowired
    private DataExportService dataExportService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @TokenAuth
    @Override
    public ExportListRspVO exportListAction(DataExportParamVO reqVO) {
        Token account = TokenThreadLocal.getToken();
        reqVO.setAccountId(String.valueOf(account.getAccountId()));
        return dataExportService.exportListAction(reqVO);
    }

    @TokenAuth
    @Override
    public String exportPvAction(DataExportParamVO reqVO) {
        Token account = TokenThreadLocal.getToken();
        reqVO.setAccountId(String.valueOf(account.getAccountId()));
        return dataExportService.exportPvAction(reqVO);
    }

    @TokenAuth
    @Override
    public String exportUvAction(DataExportParamVO reqVO) {
        return dataExportService.exportUvAction(reqVO);
    }

    @TokenAuth
    @Override
    public Boolean exportMessage(ExportMessageReqVO reqVO) {
        Token accountInfo = TokenThreadLocal.getToken();
        Integer accountId = accountInfo.getAccountId();
        return dataExportService.exportMessage(reqVO, accountId);
    }

    @TokenAuth
    @Override
    public Boolean exportReservation(@Validated ExportReservationReqVO reqVO) {
        Token accountInfo = TokenThreadLocal.getToken();
        Integer accountId = accountInfo.getAccountId();
        return dataExportService.exportReservation(reqVO, accountId);
    }
    @TokenAuth
    @Override
    public Boolean exportUvPreviewPage(ExportUvPreviewPageReqVO reqVO) {
        Token accountInfo = TokenThreadLocal.getToken();
        Integer accountId = accountInfo.getAccountId();
        return dataExportService.exportUvPreviewPage(reqVO,accountId);
    }

    @Override
    public Boolean exportWatchPvH5AddApp(DataExportParamVO reqVO) {

        assemblyAccountId(reqVO);
        return dataExportService.exportWatchPvH5AddApp(reqVO);
    }

    @Override
    public Boolean exportWatchPvApp(DataExportParamVO reqVO) {
        assemblyAccountId(reqVO);
        return dataExportService.exportWatchPvApp(reqVO);
    }

    @Override
    public Boolean exportProduct(DataExportParamVO reqVO) {
        assemblyAccountId(reqVO);
        return dataExportService.exportProduct(reqVO);
    }

    @Override
    public Boolean exportProductClick(DataExportParamVO reqVO) {
        assemblyAccountId(reqVO);
        return dataExportService.exportProductClick(reqVO);
    }

    @Override
    public Boolean exportPendant(DataExportParamVO reqVO) {
        assemblyAccountId(reqVO);
        return dataExportService.exportPendant(reqVO);
    }

    @Override
    public Boolean exportPendantClick(DataExportParamVO reqVO) {
        assemblyAccountId(reqVO);
        return dataExportService.exportPendantClick(reqVO);
    }

    @Override
    public Boolean exportplayVolumeTrend(DataExportParamVO reqVO) {
        assemblyAccountId(reqVO);
        return dataExportService.exportplayVolumeTrend(reqVO);
    }

    private void assemblyAccountId(DataExportParamVO reqVO) {
        String token = reqVO.getToken();
        Preconditions.checkArgument(StringUtils.isNotBlank(token), "token不能为空！");

        token = redisTemplate.opsForValue().get(RedisKey.AUTH_TOKEN + token);
        if (StringUtils.isBlank(token)) {
            throw new BusinessException(BizErrorCode.AUTH_ADM_TOKEN_INVALID);
        }

        //
        String tokenStr = JSON.parse(token).toString();
        Token parseObject = JSON.parseObject(tokenStr, Token.class);
        if (parseObject != null) {
            reqVO.setAccountId(parseObject.getAccountId() == null ? "0" : parseObject.getAccountId().toString());
        }
    }

}
